package org.xvsm.example;

import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.xvsm.api.core.*;
import org.xvsm.client.ClientCapi;

/**
 * Fifo Container example for mozartspaces core version 0.8.0
 */
public class Fifo {
    public static void main(String[] args) {
        try {
            Logger.getLogger("org.xvsm.client").setLevel(Level.FATAL);

            Capi capi;
                    
            if (args.length != 1) {
                System.out.println("URL needed!");
                System.out
                        .println("Usage: java -cp <classpath> org.xvsm.example.Fifo http://localhost:8080/xvsm/xvsm\n using embeded core");
            // use embeded xvsm core
              capi = CapiFactory.createCapi();

            }
            else {
            // Get an instance of the capi and initialize it.
            // Capi capi = CapiFactory.createCapi();
              capi = new ClientCapi(args[0]);
            }
            capi.init(false);
            
            // Create four tuples which will be written into the space.
            // Use the Entry.Factory to create entries with their type and
            // value.
            Entry a1 = Entry.Factory.newInstance(ValueTypes.STRING_UTF8, "ONE");
            Entry a2 = Entry.Factory.newInstance(ValueTypes.INTEGER, 1);
            // Create a new Tuple for the two Entries and add them.
            Tuple t1 = new Tuple();
            t1.addEntry(a1);
            t1.addEntry(a2);
            
            Entry b1 = Entry.Factory.newInstance(ValueTypes.STRING_UTF8, "TWO");
            Entry b2 = Entry.Factory.newInstance(ValueTypes.INTEGER, 2);
            Tuple t2 = new Tuple();
            t2.addEntry(b1);
            t2.addEntry(b2);
            
            Entry c1 = Entry.Factory.newInstance(ValueTypes.STRING_UTF8,
                    "THREE");
            Entry c2 = Entry.Factory.newInstance(ValueTypes.INTEGER, 3);
            Tuple t3 = new Tuple();
            t3.addEntry(c1);
            t3.addEntry(c2);
            
            Entry d1 = Entry.Factory
                    .newInstance(ValueTypes.STRING_UTF8, "FOUR");
            Entry d2 = Entry.Factory.newInstance(ValueTypes.INTEGER, 4);
            Tuple t4 = new Tuple();
            t4.addEntry(d1);
            t4.addEntry(d2);
            
            // Create a new Container and initialize it with Coordination Type
            // FIFO (first in first out).
            ContainerRef cref = capi.createContainer(CoordinationTypes.FIFO);
            
            // Create a List for the Entries and add them.
            List<Entry> entries = new LinkedList<Entry>();
            entries.add(t1);
            entries.add(t2);
            entries.add(t3);
            entries.add(t4);
            
            // Write the list of Entries into the created container.
            capi.write(cref, entries);
            
            // Read and delete the elements from the space.
            // NOTE: in this simple example no error handling is done. You
            // should check the arrity and the structure/type of the read tuple
            // or entries.
            for (int i = 0; i < 4; i++) {
                List<Entry> read = capi.take(cref);
                // The first element in the list is the tuple.
                Tuple t = (Tuple) read.get(0);
                // Get the entries of the tuple
                List<Entry> elements = t.getValue();
                
                System.out.println(elements.get(0).getValue() + " "
                        + elements.get(1).getValue());
            }
            
            capi.shutdown(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
